package com.zzyl.nursing.service.impl;

import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-21
 */
@Transactional
@Service
public class NursingPlanServiceImpl implements INursingPlanService 
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();

        //查询护理计划 根据护理计划ID
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        //根据护理计划ID 查询护理项目的关系
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional      //作用：将当前方法交给 spring 进行事务管理。·方法执行前，开启事务；·成功执行完毕，提交事务；·出现异常，回滚事务
    @Override
    public int insertNursingPlan(NursingPlanDto dto)
    {
        NursingPlan nursingPlan = nursingPlanMapper.selectByPlanName(dto.getPlanName());
        if (nursingPlan != null) {
            throw new BaseException("护理计划已存在");
        }
        //1. 保存护理计划：目的 --> 获取计划的主键ID，用以后面保存“护理计划与项目的关系”时，能把项目与计划对应起来。否则，空有项目，但不知道是哪个计划的项目
        nursingPlan = new NursingPlan();    // 若计划为新计划，则重新为其赋空值
            /*  想办法把dto中的各种数据，传给数据库中，这里先把能存进NursingPlan的数据，先存完：
                nursingPlan.setPlanName(dto.getPlanName());
                nursingPlan.setStatus(dto.getStatus());
                nursingPlan.setSortNo(dto.getSortNo());*/
        //属性拷贝(简便方法)
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);   // 这里在数据库中插入保存“护理计划”的数据后，会生成对应的主键ID返回
        // 获取计划的主键ID
        Long planId = nursingPlan.getId();

        //2. 批量保存护理计划与项目的关系   nursing_project_plan
        int count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), planId);   // count --> 影响的行数
        return count;   // 不为0，则表示保存成功
    }

    /**
     /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto dto) {

        if(dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0){
            //删除护理项目和计划的中间关系  根据计划id删除
            nursingProjectPlanMapper.deleteNursingProjectPlanById(dto.getId());

            //批量新增中间关系
            nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(),dto.getId());
        }

        //修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        return nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Long id)
    {
        nursingProjectPlanMapper.deleteNursingProjectPlanById(id);  // 删除护理计划 与 护理项目的关联表
        return nursingPlanMapper.deleteNursingPlanById(id);         // 删除护理计划
    }
}
